package com.example.smart_city_0829.activity.volunteering;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;

import com.example.smart_city_0829.R;
import com.example.smart_city_0829.activity.volunteering.fragment.MyVolunteeringFragment;
import com.example.smart_city_0829.activity.volunteering.fragment.VolunteeringMainFragment;
import com.example.smart_city_0829.adapter.BaseFragmentViewPagerAdapter;
import com.example.smart_city_0829.base.BaseActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.Arrays;

public class VolunteeringMainActivity extends BaseActivity {

    private ViewPager vp;
    private BottomNavigationView bottomNavi;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_volunteering_main;
    }

    @Override
    protected void initView() {

        vp = (ViewPager) findViewById(R.id.vp);
        bottomNavi = (BottomNavigationView) findViewById(R.id.bottom_navi);
    }

    @Override
    protected void initData() {
        vp.setAdapter(
                new BaseFragmentViewPagerAdapter(
                        getSupportFragmentManager(),
                        0,
                        Arrays.asList(
                                VolunteeringMainFragment.newInstance(null, null),
                                MyVolunteeringFragment.newInstance(null, null)
                        ))
        );

    }

    @Override
    protected void initEvent() {
        bottomNavi.setOnItemSelectedListener(
                item -> {
                    switch (item.getItemId()) {
                        case R.id.main:
                            vp.setCurrentItem(0);
                            return true;
                        case R.id.mine:
                            vp.setCurrentItem(1);
                            return true;
                    }
                    return false;
                }
        );

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //ignore
            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        bottomNavi.setSelectedItemId(R.id.main);
                        break;
                    case 1:
                        bottomNavi.setSelectedItemId(R.id.mine);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //ignore
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "志愿活动";
    }
}